package com.yonyou.crm.sact.outwork.appctrl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.common.security.util.Md5CodeUtil;
import com.yonyou.crm.sact.outwork.entity.RuleTimeListVO;
import com.yonyou.crm.sact.outwork.entity.TraceListVO;
import com.yonyou.crm.sact.outwork.entity.TraceVO;
import com.yonyou.crm.sact.outwork.rmitf.IOutworkRmService;

@RestController
@RequestMapping(value="/{version}/msact/outworks")
public class AppOutworkCtrl {
	
	@Autowired
	private IOutworkRmService service;
	
	@RequestMapping(value = "rules", method = RequestMethod.GET)
	public Object getRule(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String appversion = paramObj.getString("version");
		Object dateObj = paramObj.get("date");
		Map<String, Object> paraMap = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (dateObj == null) {
			Date today = new Date();
			paraMap.put("date", sdf.format(today));
		} else {
			paraMap.put("date",  sdf.format(dateObj));
		}
		
		List<RuleTimeListVO> ruleList = service.getRuleList(paraMap);
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("rulelist", ruleList);
		
		StringBuffer desc = new StringBuffer();
		desc.append("1.外勤签到成功，自动显示在动态 .  2.");
		if (ruleList.isEmpty()) {
			desc.append("您无签到时间要求，但需签到.");
		} else {
			String d = "您的签到时间要求为上午" + ruleList.get(0).getStarttime() + "到" + ruleList.get(0).getEndtime()
					    + "、下午" + ruleList.get(1).getStarttime() + "到" + ruleList.get(1).getEndtime() + "，规定时间内至少签到一次，其他时间不限";
			desc.append(d);
		}
		data.put("description", desc.toString());
		
		String hashCodeVersion = Md5CodeUtil.createMd5CodeForMap(data);
		
		Map<String, Object> rsMap = new HashMap<String, Object>();
		if (StringUtils.isBlank(appversion) || (!appversion.equals(hashCodeVersion))) {
			rsMap = data;
		}
		rsMap.put("version", hashCodeVersion);
		
		return ResponseUtil.getObject(rsMap);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
	public Object save(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Map<String, Object> gpsinfo = (Map<String, Object>)paramObj.get("gpsinfo");
		String note = paramObj.getString("note");
		List<String> picList = (List<String>)paramObj.get("piclist");
		
		TraceVO vo = new TraceVO();
		vo.setAddress(gpsinfo.get("address").toString());
		vo.setLongitude(gpsinfo.get("longitude").toString());
		vo.setLatitude(gpsinfo.get("latitude").toString());
		vo.setAltitude(gpsinfo.get("altitude").toString());
		
		vo.setNote(note);
		StringBuffer pics = new StringBuffer();
		int picNum = picList.size();
		if (picNum == 0) {
			vo.setNoteFile("");
			vo.setNoteType(1);
		} else {
			for (int i = 0; i < picNum; i++) {
				if (i > 0) {
					pics.append(",");
				}
				pics.append(picList.get(i));
			}
			vo.setNoteFile(pics.toString());
			vo.setNoteType(2);
		}
		
		service.saveTrace(vo);
		
		return ResponseUtil.getObject();
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public Object getTraceList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String dateStr = paramObj.getString("date");
		String userId = paramObj.getString("userid");
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date startTime = sdf.parse(dateStr + " 00:00:00");
			Date endTime = sdf.parse(dateStr + " 23:59:59");
			paraMap.put("startTime", startTime);
			paraMap.put("endTime", endTime);
		} catch (ParseException e) {
			
		}
		if (!userId.isEmpty()) {
			paraMap.put("userId", userId);
		}
		
		List<TraceListVO> outworkList = service.getTraceList(paraMap);
		
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("outworkslist", outworkList);
		return ResponseUtil.getObject(rsMap);
	}
	
	@RequestMapping(value="absence", method = RequestMethod.POST)
	public Object getAbsenceDates(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String startdate = paramObj.getString("startdate");
		String enddate = paramObj.getString("enddate");
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("startDate", startdate);
		paraMap.put("endDate", enddate);
		
		List<String> list = service.getAbsenceDates(paraMap);
		
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("datelist", list);
		return ResponseUtil.getObject(rsMap);
	}
	
	@RequestMapping(value="subworks", method = RequestMethod.GET)
	public Object getSubOutworkList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		
		String dateStr = paramObj.getString("date");
		paraMap.put("startDate", dateStr);
		paraMap.put("endDate", dateStr);
		
		@SuppressWarnings("unchecked")
		List<String> userList = (List<String>)paramObj.get("userlist");
		
		paraMap.put("userList", userList);
		
		Map<String, Object> rsMap = service.getSubOutworkList(paraMap);
		return ResponseUtil.getObject(rsMap);
	}

}
